using System;
using System.Drawing;
using System.Windows.Forms;
using FridayShopping.DataModel;

namespace FridayShopping.UI
{
	/// <summary>
	/// Summary description for ItemPropertiesForm.
	/// </summary>
	public class ProductPropertiesForm : System.Windows.Forms.Form, IItemPropertiesForm
	{
		private System.Windows.Forms.MainMenu mainMenu1;

		private Product			product;
		private Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.Label lbl_Name;
		private System.Windows.Forms.TextBox edit_Name;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cb_categories;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox edit_Price;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.NumericUpDown numeric_usual;
		private System.Windows.Forms.Button btn_Cancel;
		private System.Windows.Forms.TextBox edit_Notes;
		private Categories		categories;
	
		public ProductPropertiesForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		public ProductPropertiesForm(Product product)
		{
			this.product = product;

			InitializeComponent();

			// Fill the category combobox
			categories = Database.Instance.Categories;

			cb_categories.DataSource = categories.List;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ProductPropertiesForm));
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.lbl_Name = new System.Windows.Forms.Label();
			this.edit_Name = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.cb_categories = new System.Windows.Forms.ComboBox();
			this.label3 = new System.Windows.Forms.Label();
			this.edit_Price = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.numeric_usual = new System.Windows.Forms.NumericUpDown();
			this.btn_Cancel = new System.Windows.Forms.Button();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.edit_Notes = new System.Windows.Forms.TextBox();
			// 
			// inputPanel1
			// 
			this.inputPanel1.EnabledChanged += new System.EventHandler(this.inputPanel1_EnabledChanged);
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.Add(this.tabPage1);
			this.tabControl1.Controls.Add(this.tabPage2);
			this.tabControl1.Enabled = ((bool)(resources.GetObject("tabControl1.Enabled")));
			this.tabControl1.Font = ((System.Drawing.Font)(resources.GetObject("tabControl1.Font")));
			this.tabControl1.Location = ((System.Drawing.Point)(resources.GetObject("tabControl1.Location")));
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = ((System.Drawing.Size)(resources.GetObject("tabControl1.Size")));
			this.tabControl1.Text = resources.GetString("tabControl1.Text");
			this.tabControl1.Visible = ((bool)(resources.GetObject("tabControl1.Visible")));
			this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.Add(this.lbl_Name);
			this.tabPage1.Controls.Add(this.edit_Name);
			this.tabPage1.Controls.Add(this.label1);
			this.tabPage1.Controls.Add(this.cb_categories);
			this.tabPage1.Controls.Add(this.label3);
			this.tabPage1.Controls.Add(this.edit_Price);
			this.tabPage1.Controls.Add(this.label2);
			this.tabPage1.Controls.Add(this.numeric_usual);
			this.tabPage1.Controls.Add(this.btn_Cancel);
			this.tabPage1.Enabled = ((bool)(resources.GetObject("tabPage1.Enabled")));
			this.tabPage1.Location = ((System.Drawing.Point)(resources.GetObject("tabPage1.Location")));
			this.tabPage1.Size = ((System.Drawing.Size)(resources.GetObject("tabPage1.Size")));
			this.tabPage1.Text = resources.GetString("tabPage1.Text");
			// 
			// lbl_Name
			// 
			this.lbl_Name.Enabled = ((bool)(resources.GetObject("lbl_Name.Enabled")));
			this.lbl_Name.Font = ((System.Drawing.Font)(resources.GetObject("lbl_Name.Font")));
			this.lbl_Name.Location = ((System.Drawing.Point)(resources.GetObject("lbl_Name.Location")));
			this.lbl_Name.Size = ((System.Drawing.Size)(resources.GetObject("lbl_Name.Size")));
			this.lbl_Name.Text = resources.GetString("lbl_Name.Text");
			this.lbl_Name.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lbl_Name.TextAlign")));
			this.lbl_Name.Visible = ((bool)(resources.GetObject("lbl_Name.Visible")));
			// 
			// edit_Name
			// 
			this.edit_Name.Enabled = ((bool)(resources.GetObject("edit_Name.Enabled")));
			this.edit_Name.Font = ((System.Drawing.Font)(resources.GetObject("edit_Name.Font")));
			this.edit_Name.Location = ((System.Drawing.Point)(resources.GetObject("edit_Name.Location")));
			this.edit_Name.MaxLength = ((int)(resources.GetObject("edit_Name.MaxLength")));
			this.edit_Name.Multiline = ((bool)(resources.GetObject("edit_Name.Multiline")));
			this.edit_Name.PasswordChar = ((char)(resources.GetObject("edit_Name.PasswordChar")));
			this.edit_Name.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("edit_Name.ScrollBars")));
			this.edit_Name.Size = ((System.Drawing.Size)(resources.GetObject("edit_Name.Size")));
			this.edit_Name.Text = resources.GetString("edit_Name.Text");
			this.edit_Name.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("edit_Name.TextAlign")));
			this.edit_Name.Visible = ((bool)(resources.GetObject("edit_Name.Visible")));
			this.edit_Name.WordWrap = ((bool)(resources.GetObject("edit_Name.WordWrap")));
			this.edit_Name.GotFocus += new System.EventHandler(this.edit_Name_GotFocus);
			// 
			// label1
			// 
			this.label1.Enabled = ((bool)(resources.GetObject("label1.Enabled")));
			this.label1.Font = ((System.Drawing.Font)(resources.GetObject("label1.Font")));
			this.label1.Location = ((System.Drawing.Point)(resources.GetObject("label1.Location")));
			this.label1.Size = ((System.Drawing.Size)(resources.GetObject("label1.Size")));
			this.label1.Text = resources.GetString("label1.Text");
			this.label1.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("label1.TextAlign")));
			this.label1.Visible = ((bool)(resources.GetObject("label1.Visible")));
			// 
			// cb_categories
			// 
			this.cb_categories.DisplayMember = "Name";
			this.cb_categories.Enabled = ((bool)(resources.GetObject("cb_categories.Enabled")));
			this.cb_categories.Font = ((System.Drawing.Font)(resources.GetObject("cb_categories.Font")));
			this.cb_categories.Location = ((System.Drawing.Point)(resources.GetObject("cb_categories.Location")));
			this.cb_categories.Size = ((System.Drawing.Size)(resources.GetObject("cb_categories.Size")));
			this.cb_categories.Text = resources.GetString("cb_categories.Text");
			this.cb_categories.ValueMember = "Key";
			this.cb_categories.Visible = ((bool)(resources.GetObject("cb_categories.Visible")));
			// 
			// label3
			// 
			this.label3.Enabled = ((bool)(resources.GetObject("label3.Enabled")));
			this.label3.Font = ((System.Drawing.Font)(resources.GetObject("label3.Font")));
			this.label3.Location = ((System.Drawing.Point)(resources.GetObject("label3.Location")));
			this.label3.Size = ((System.Drawing.Size)(resources.GetObject("label3.Size")));
			this.label3.Text = resources.GetString("label3.Text");
			this.label3.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("label3.TextAlign")));
			this.label3.Visible = ((bool)(resources.GetObject("label3.Visible")));
			// 
			// edit_Price
			// 
			this.edit_Price.Enabled = ((bool)(resources.GetObject("edit_Price.Enabled")));
			this.edit_Price.Font = ((System.Drawing.Font)(resources.GetObject("edit_Price.Font")));
			this.edit_Price.Location = ((System.Drawing.Point)(resources.GetObject("edit_Price.Location")));
			this.edit_Price.MaxLength = ((int)(resources.GetObject("edit_Price.MaxLength")));
			this.edit_Price.Multiline = ((bool)(resources.GetObject("edit_Price.Multiline")));
			this.edit_Price.PasswordChar = ((char)(resources.GetObject("edit_Price.PasswordChar")));
			this.edit_Price.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("edit_Price.ScrollBars")));
			this.edit_Price.Size = ((System.Drawing.Size)(resources.GetObject("edit_Price.Size")));
			this.edit_Price.Text = resources.GetString("edit_Price.Text");
			this.edit_Price.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("edit_Price.TextAlign")));
			this.edit_Price.Visible = ((bool)(resources.GetObject("edit_Price.Visible")));
			this.edit_Price.WordWrap = ((bool)(resources.GetObject("edit_Price.WordWrap")));
			// 
			// label2
			// 
			this.label2.Enabled = ((bool)(resources.GetObject("label2.Enabled")));
			this.label2.Font = ((System.Drawing.Font)(resources.GetObject("label2.Font")));
			this.label2.Location = ((System.Drawing.Point)(resources.GetObject("label2.Location")));
			this.label2.Size = ((System.Drawing.Size)(resources.GetObject("label2.Size")));
			this.label2.Text = resources.GetString("label2.Text");
			this.label2.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("label2.TextAlign")));
			this.label2.Visible = ((bool)(resources.GetObject("label2.Visible")));
			// 
			// numeric_usual
			// 
			this.numeric_usual.Enabled = ((bool)(resources.GetObject("numeric_usual.Enabled")));
			this.numeric_usual.Location = ((System.Drawing.Point)(resources.GetObject("numeric_usual.Location")));
			this.numeric_usual.Size = ((System.Drawing.Size)(resources.GetObject("numeric_usual.Size")));
			this.numeric_usual.Visible = ((bool)(resources.GetObject("numeric_usual.Visible")));
			// 
			// btn_Cancel
			// 
			this.btn_Cancel.Enabled = ((bool)(resources.GetObject("btn_Cancel.Enabled")));
			this.btn_Cancel.Font = ((System.Drawing.Font)(resources.GetObject("btn_Cancel.Font")));
			this.btn_Cancel.Location = ((System.Drawing.Point)(resources.GetObject("btn_Cancel.Location")));
			this.btn_Cancel.Size = ((System.Drawing.Size)(resources.GetObject("btn_Cancel.Size")));
			this.btn_Cancel.Text = resources.GetString("btn_Cancel.Text");
			this.btn_Cancel.Visible = ((bool)(resources.GetObject("btn_Cancel.Visible")));
			this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.Add(this.edit_Notes);
			this.tabPage2.Enabled = ((bool)(resources.GetObject("tabPage2.Enabled")));
			this.tabPage2.Location = ((System.Drawing.Point)(resources.GetObject("tabPage2.Location")));
			this.tabPage2.Size = ((System.Drawing.Size)(resources.GetObject("tabPage2.Size")));
			this.tabPage2.Text = resources.GetString("tabPage2.Text");
			// 
			// edit_Notes
			// 
			this.edit_Notes.Enabled = ((bool)(resources.GetObject("edit_Notes.Enabled")));
			this.edit_Notes.Font = ((System.Drawing.Font)(resources.GetObject("edit_Notes.Font")));
			this.edit_Notes.Location = ((System.Drawing.Point)(resources.GetObject("edit_Notes.Location")));
			this.edit_Notes.MaxLength = ((int)(resources.GetObject("edit_Notes.MaxLength")));
			this.edit_Notes.Multiline = ((bool)(resources.GetObject("edit_Notes.Multiline")));
			this.edit_Notes.PasswordChar = ((char)(resources.GetObject("edit_Notes.PasswordChar")));
			this.edit_Notes.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("edit_Notes.ScrollBars")));
			this.edit_Notes.Size = ((System.Drawing.Size)(resources.GetObject("edit_Notes.Size")));
			this.edit_Notes.Text = resources.GetString("edit_Notes.Text");
			this.edit_Notes.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("edit_Notes.TextAlign")));
			this.edit_Notes.Visible = ((bool)(resources.GetObject("edit_Notes.Visible")));
			this.edit_Notes.WordWrap = ((bool)(resources.GetObject("edit_Notes.WordWrap")));
			this.edit_Notes.GotFocus += new System.EventHandler(this.edit_Notes_GotFocus);
			// 
			// ProductPropertiesForm
			// 
			this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
			this.Controls.Add(this.tabControl1);
			this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));
			this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));
			this.Menu = this.mainMenu1;
			this.Text = resources.GetString("$this.Text");
			this.Visible = ((bool)(resources.GetObject("$this.Visible")));
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ItemPropertiesForm_Closing);
			this.Load += new System.EventHandler(this.ItemPropertiesForm_Load);

		}
		#endregion

		#region IItemPropertiesForm Members
		public ItemBase GetItem(EditMode mode)
		{
			if (product == null && mode == EditMode.Add) 
			{
				product = new Product();
			}
			// Read values from the form
			product.Name = edit_Name.Text;
			product.CategoryKey = (Key)cb_categories.SelectedValue;
			product.Usual = (int)numeric_usual.Value;
			if (edit_Price.Text.Trim().Length == 0) 
			{
				product.Price = 0;
			} 
			else 
			{
				try 
				{
					product.Price = (float)float.Parse(edit_Price.Text);
				}
				catch (FormatException)
				{
					product.Price = 0;
				}
			}
			product.Memo = edit_Notes.Text;

			return product;
		}

		public void SetDefaultValue(object obj)
		{
			cb_categories.SelectedValue = obj;
		}

		#endregion

		#region Form Event Handlers
		private void ItemPropertiesForm_Load(object sender, System.EventArgs e)
		{
			if (product != null) 
			{
				edit_Name.Text = product.Name;
				cb_categories.SelectedValue = product.CategoryKey;
				numeric_usual.Value = product.Usual;
				edit_Price.Text = product.Price.ToString();
				edit_Notes.Text = product.Memo;
			}
			else 
			{
				if (cb_categories.SelectedValue == null) 
				{
					cb_categories.SelectedIndex = -1;
				}
			}
		}

		private void btn_Cancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		private void ItemPropertiesForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (this.DialogResult != DialogResult.Cancel)
			{
				string name = edit_Name.Text;
				if (name.Trim().Length == 0) 
				{
					MessageBox.Show(LangResources.Instance.Resources.GetString("emptyItemName"), 
						LangResources.Instance.Resources.GetString("applName"));
					e.Cancel = true;
					return;
				}
				if (cb_categories.SelectedValue == null)
				{
					MessageBox.Show(LangResources.Instance.Resources.GetString("categoryWasNotSelected"), 
						LangResources.Instance.Resources.GetString("applName"));
					e.Cancel = true;
					return;
				}
			}
			inputPanel1.Enabled = false;
		}

		private void edit_Name_GotFocus(object sender, System.EventArgs e)
		{
			inputPanel1.Enabled = true;
		}

		private void edit_Price_GotFocus(object sender, System.EventArgs e)
		{
			inputPanel1.Enabled = true;
		}

		private void edit_Notes_GotFocus(object sender, System.EventArgs e)
		{
			inputPanel1.Enabled = true;
		}

		private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (tabControl1.SelectedIndex == 1) 
			{
				edit_Notes.Focus();
			}
		}

		private void inputPanel1_EnabledChanged(object sender, System.EventArgs e)
		{
			Rectangle rect = inputPanel1.Bounds;

			int newHeight = tabControl1.Height;

			if (inputPanel1.Enabled) 
			{
				if (rect.Top < tabControl1.Bounds.Bottom) 
				{
					newHeight = tabControl1.Size.Height - rect.Height;
				}
			} 
			else 
			{
				newHeight = tabControl1.Size.Height + rect.Height;
			}
			tabControl1.Size = new Size(tabControl1.Size.Width, newHeight);
		}
		#endregion
	}
}
